"""
1 寻找上传的文件
2 用linux命令解压到固定文件夹下
3 扫描此文件夹下的所有文件
4 获取这些文件的文件名，文件大小，作为返回值
5 用uuid重命名
6 移到指定目录下
7 删除此文件夹
"""
import os
import uuid

def subfile(file_path, new_file):
    """
    處理解壓出來的文件
    :param file_path: 放壓縮包的路徑，
    :param new_file: 臨時解壓路徑,最後將刪除
    :return:{file_name:{file_size:file_new_name}}
    """
    for rsub, ssub, sfile in os.walk(new_file):
        k = {}
        for f in sfile:
            f = str(f).replace(' ', '')
            s = os.path.getsize(os.path.join(rsub, f))
            spfile = str(f).split('.')
            new_name = str(uuid.uuid1()) + '.' + spfile[-1]
            file_dict = {f: {s: str(file_path + new_name)}}
            k.update(file_dict)
            os.system('cd {2}; mv {0} {1}'.format(f, new_name, new_file))
        os.system('cd {0}; mv {0}/* {1}'.format(new_file, file_path))
        os.system('cd {0}; rm -rf {1}'.format(file_path, new_file))
        return k

def build_linux_cmd(file_path, file_name, new_file, end):
    """
    構造linux命令
    :param file_path:
    :param file_name:
    :param new_file:
    :param end:
    :return:
    """
    all_file_info = []
    if end == 'rar':
        # new_file = file_path + '/' + str(i)#創建臨時解壓路徑，防止解壓出來的文件重名，此路徑完成臨時存放解壓出來的文件後將被刪除
        os.system('cd {0};mkdir {2};unrar e {0}/{1} {2}'.format(file_path, file_name, new_file))  # 解壓文件
        res_rar = subfile(file_path=target_path, new_file=new_file)  # 獲取解壓出來的文件的原文件名，文件大小，新文件名
        all_file_info.append(res_rar)
        # for rsub, ssub, sfile in os.walk(new_file):
        #     for f in sfile:
        #         s = os.path.getsize(os.path.join(rsub, f))
        #         spfile = str(f).split('.')
        #         new_name = str(uuid.uuid1()) + '.' + spfile[-1]
        #         file_dict = {f:{s:str(file_path + new_name)}}
        #         all_file_info.append(file_dict)
        #         os.system('cd {2}; mv {0} {1}'.format(f, new_name, new_file))
        #     os.system('cd {0}; mv ./* {1}'.format(new_file, file_path))
        #     os.system('cd {0}; rm -rf {1}'.format(file_path, new_file))
    elif end == 'zip':
        # new_file = target_path + '/' + str(i)
        os.system('cd {0}; mkdir {2}; unzip {0}/{1} -d {2} '.format(file_path, file_name, new_file))
        res_zip = subfile(file_path=target_path, new_file=new_file)
        all_file_info.append(res_zip)
    elif end == '7z':
        os.system('cd {0}; mkdir {2}; 7za X {0}/{1} -o{2} '.format(file_path, file_name, new_file))
        res_7z = subfile(file_path=target_path, new_file=new_file)
        all_file_info.append(res_7z)
    elif end == 'gz':
        os.system('cd {0}; mkdir {2}; tar -zxvf {0}/{1} -C {2}'.format(file_path, file_name, new_file))
        res_gz = subfile(file_path=target_path, new_file=new_file)
        all_file_info.append(res_gz)
    return all_file_info


def unfile(file_path, target_path):
    """
    功能：將file_path路徑下的rar和zip解壓到file_path並且用uuid重命名
    :param file_path:
    :return: all_file_info
    """
    if os.path.isdir(file_path):
        all_file_info = []
        for root, sub, file in os.walk(file_path):
            for i, file_name in enumerate(file):
                end = file_name.split('.')[-1]
                if target_path:
                    new_file = target_path + '/' + str(i)
                    file_info = build_linux_cmd(file_path=file_path, file_name=file_name,new_file=new_file, end=end)
                    # if end == 'rar':
                    #     # new_file = file_path + '/' + str(i)#創建臨時解壓路徑，防止解壓出來的文件重名，此路徑完成臨時存放解壓出來的文件後將被刪除
                    #     os.system('cd {0};mkdir {2};unrar e {0}/{1} {2}'.format(file_path, file_name, new_file))#解壓文件
                    #     res_rar = subfile(file_path=target_path, new_file=new_file)#獲取解壓出來的文件的原文件名，文件大小，新文件名
                    #     all_file_info.append(res_rar)
                    #     # for rsub, ssub, sfile in os.walk(new_file):
                    #     #     for f in sfile:
                    #     #         s = os.path.getsize(os.path.join(rsub, f))
                    #     #         spfile = str(f).split('.')
                    #     #         new_name = str(uuid.uuid1()) + '.' + spfile[-1]
                    #     #         file_dict = {f:{s:str(file_path + new_name)}}
                    #     #         all_file_info.append(file_dict)
                    #     #         os.system('cd {2}; mv {0} {1}'.format(f, new_name, new_file))
                    #     #     os.system('cd {0}; mv ./* {1}'.format(new_file, file_path))
                    #     #     os.system('cd {0}; rm -rf {1}'.format(file_path, new_file))
                    # elif end == 'zip':
                    #     # new_file = target_path + '/' + str(i)
                    #     os.system('cd {0}; mkdir {2}; unzip {0}/{1} -d {2} '.format(file_path, file_name, new_file))
                    #     res_zip = subfile(file_path=target_path, new_file=new_file)
                    #     all_file_info.append(res_zip)
                    # elif end == '7z':
                    #     os.system('cd {0}; mkdir {2}; 7za X {0}/{1} -o{2} '.format(file_path, file_name, new_file))
                    #     res_7z = subfile(file_path=target_path, new_file=new_file)
                    #     all_file_info.append(res_7z)
                    # elif end == 'gz':
                    #     os.system('cd {0}; mkdir {2}; tar -zxvf {0}/{1} -C {2}'.format(file_path, file_name, new_file))
                    #     res_gz = subfile(file_path=target_path, new_file=new_file)
                    #     all_file_info.append(res_gz)
                else:
                    new_file = file_path + '/' + str(i)  # 創建臨時解壓路徑，防止解壓出來的文件重名，此路徑完成臨時存放解壓出來的文件後將被刪除
                    file_info = build_linux_cmd(file_path=file_path, file_name=file_name, new_file=new_file, end=end)
                    # if end == 'rar':
                    #     os.system('cd {0};mkdir {2};unrar e {0}/{1} {2}'.format(file_path, file_name, new_file))#解壓文件
                    #     res_rar = subfile(file_path=file_path, new_file=new_file)#獲取解壓出來的文件的原文件名，文件大小，新文件名
                    #     all_file_info.append(res_rar)
                    #     # for rsub, ssub, sfile in os.walk(new_file):
                    #     #     for f in sfile:
                    #     #         s = os.path.getsize(os.path.join(rsub, f))
                    #     #         spfile = str(f).split('.')
                    #     #         new_name = str(uuid.uuid1()) + '.' + spfile[-1]
                    #     #         file_dict = {f:{s:str(file_path + new_name)}}
                    #     #         all_file_info.append(file_dict)
                    #     #         os.system('cd {2}; mv {0} {1}'.format(f, new_name, new_file))
                    #     #     os.system('cd {0}; mv ./* {1}'.format(new_file, file_path))
                    #     #     os.system('cd {0}; rm -rf {1}'.format(file_path, new_file))
                    # elif end == 'zip':
                    #     os.system('cd {0}; mkdir {2}; unzip {0}/{1} -d {2} '.format(file_path, file_name, new_file))
                    #     res_zip = subfile(file_path=file_path, new_file=new_file)
                    #     all_file_info.append(res_zip)
                    # elif end == '7z':
                    #     os.system('cd {0}; mkdir {2}; 7za X {0}/{1} -o{2} '.format(file_path, file_name, new_file))
                    #     res_7z = subfile(file_path=target_path, new_file=new_file)
                    #     all_file_info.append(res_7z)
                    #
                    # elif end == 'gz':
                    #     os.system('cd {0}; mkdir {2}; tar -zxvf {0}/{1} -C {2}'.format(file_path, file_name, new_file))
                    #     res_gz = subfile(file_path=target_path, new_file=new_file)
                    #     all_file_info.append(res_gz)
                all_file_info.append(file_info)
        print(all_file_info)


if __name__ == '__main__':
    file_path = '/root/unfile_one'
    target_path = '/root/unfile_two'
    res = unfile(file_path, target_path)
    print(res)
    # print(res1, res2, res3)
    # for i in linux_cmd:
    #     print(os.system(i))
    # print(linux_cmd)
    # linux_cmd = ['unrar e /root/unfile/Desktop.rar /root/unfile/', 'unzip /root/unfile/Desktop.zip -d /root/unfile/']
    # for i in linux_cmd:
    #     print(os.system(i))
    # m = ['cd /root', 'ls -al']
    # for i in m:
    #     print(os.system(i))
    # print(os.system('cd /root/unfile; mkdir faith; unrar e ./we.rar ./faith; cd ./faith; ls -al'))
    # for a, b, c in os.walk('/root/unfile/faith'):
    #     for f in c:
    #         s = os.path.getsize(os.path.join(a,f))
    #         print(f,s)